<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-data">&nbsp;&nbsp;财务管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;固定资产添加</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">数据添加</el-divider>
        <el-form
            :model="fixedAssetsForm"
            :rules="rules"
            ref="fixedAssetsForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="资产编号:" prop="assetId">
                <el-input v-model="fixedAssetsForm.assetId" placeholder="请输入资产编号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="资产分类:" prop="assetClassification">
                <el-select v-model="fixedAssetsForm.assetClassification" placeholder="请选择分类">
                  <el-option
                      v-for="item in assetClassifications"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="资产名称:" prop="assetName">
            <el-input v-model="fixedAssetsForm.assetName" placeholder="输入资产名称"></el-input>
          </el-form-item>
          <el-form-item label="资产原值:" prop="originValueAsset">
            <el-input v-model="fixedAssetsForm.originValueAsset" placeholder="输入资产原值"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="购置日期:" prop="dateAcquisition">
                <el-date-picker
                    v-model="fixedAssetsForm.dateAcquisition"
                    type="date"
                    format="yyyy 年 MM 月 dd 日"
                    value-format="yyyy-MM-dd"
                    placeholder="选择购置日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="规格型号:" prop="specificationModel">
                <el-input v-model="fixedAssetsForm.specificationModel" placeholder="输入规格型号"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="负责人:" prop="responsePerson">
            <el-input v-model="fixedAssetsForm.responsePerson" placeholder="输入负责人"></el-input>
          </el-form-item>
          <el-form-item label="备注:" prop="remarks">
            <el-input v-model="fixedAssetsForm.remarks" placeholder="输入备注"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('fixedAssetsForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('fixedAssetsForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fixedAssetsForm: {
        assetId: 12,
        assetName: "铅笔",
        assetClassification: "办公设备",
        originValueAsset: 12000,
        dateAcquisition: '2022-01-01',
        specificationModel: "晨光",
        responsePerson: "阿杰",
        remarks: "一般财务",
      },
      assetClassifications: [
        {
          value: "数码设备",
          label: "数码设备"
        },
        {
          value: "机器设备",
          label: "机器设备"
        },
        {
          value: "办公设备",
          label: "办公设备"
        },
        {
          value: "房屋建筑",
          label: "房屋建筑"
        },
        {
          value: "公司财产",
          label: "公司财产"
        },
      ],
      rules: {
        assetId: [
          {required:true,message:"请输入编号",trigger:'blur'},
          {pattern:/^[0-9]+$/,message: "请输入数字",trigger: 'blur'}
        ],
        assetName: [
          {required:true,message:"请输入资产名称",trigger:'blur'},
          {pattern:/[\u4E00-\u9FA5]/,message: "请输入中文",trigger: 'blur'}
        ],
        assetClassification: [
          {required: true, message: "请选择资产分类", trigger: 'change'}
        ],
        originValueAsset: [
          {required:true,message:"请输入资产原值",trigger:'blur'},
          {pattern:/^[0-9]+$/,message: "请输入数字",trigger: 'blur'}
        ],
        dateAcquisition: [
            {required: true, message: "请选择日期", trigger: 'change'}
        ],
        specificationModel: [
          {required: true, message: "请输入规格", trigger: 'blur'},
          {pattern: /^[\u4E00-\u9FA5A-Za-z0-9_]+$/, message: "请输入中英文数字", trigger: 'blur'}
        ],
        responsePerson: [
          {required: true, message: "请输入负责人", trigger: 'blur'},
          {pattern:/[\u4E00-\u9FA5]/,message: "请输入中文",trigger: 'blur'}
        ],
        remarks: [
          {required:true,message:"请输入备注",trigger:'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ]
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/financeFixedAssets/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.fixedAssetsForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加成功！',
                type: 'success'
              });
              location.reload();
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

</style>